home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / OpenLibs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  5.7 KB  |  244 lines

  1. //#define __USE_SYSBASE
  2. #include <proto/exec.h>
  3.  
  4. #include <clib/extras/exec_protos.h>
  5. #include <extras/libs.h>
  6.  
  7.  
  8. #include <proto/intuition.h>
  9.  
  10. #include <string.h>
  11. #include <exec/memory.h>
  12. #include <stdio.h>
  13. #include "libcode.h" // defines LIBCODE
  14.  
  15. void NeedLibs_WB(STRPTR ProgName, 
  16.               STRPTR ErrorString, 
  17.               STRPTR LibVerFmt, 
  18.               STRPTR ButtonText, 
  19.               struct Libs *Libs);
  20.               
  21. void NeedLibs_CLI(STRPTR ProgName, 
  22.               STRPTR ErrorString, 
  23.               STRPTR LibVerFmt, 
  24.               struct Libs *Libs);
  25.  
  26.  
  27. /****** extras.lib/ex_OpenLibs ******************************************
  28. *
  29. *   NAME
  30. *       ex_OpenLibs -- attempt to open multiple libraries.
  31. *
  32. *   SYNOPSIS
  33. *       success = ex_OpenLibs(Argc,ProgName,ErrorStr,
  34. *                   LibVerFmt,ButtonText,Libs)
  35. *
  36. *       BOOL ex_OpenLibs(ULONG Argc, STRPTR ProgName, STRPTR ErrorString, 
  37. *              STRPTR LibVerFmt, STRPTR ButtonText, struct Libs *Libs);
  38. *
  39. *   FUNCTION
  40. *       Attempt to open multiple Libraries.  If any library
  41. *       fails to open, a requester is opened to notify the 
  42. *       user listing all of the libraries that failed to open. 
  43. *
  44. *   INPUTS
  45. *       Argc       - argc from main()
  46. *       ProgName   - pointer to a string containing the 
  47. *                   program's name.
  48. *       ErrorStr   - error string.  May be NULL.  Defaults to
  49. *                   "The following libraries are required:\n";
  50. *       LibVerFmt  - printf style format string.  Defaults to 
  51. *                    "  %s version %ld\n"
  52. *       ButtonText - If a requester is used to display an error
  53. *                    message, this text is used in the button.
  54. *                    Defaults to "Ok"
  55. *       Libs       - an array of libraries to open.
  56. *
  57. *   RESULT
  58. *       return 0 on failure and non-zero on success.
  59. *
  60. *   EXAMPLE
  61. *       struct IntuitionBase *IntuitionBase;
  62. *       struct GfxBase *GfxBase;
  63. *       struct Library *GadToolsBase;
  64. *       
  65. *       struct Libs MyLibs[]=
  66. *       {
  67. *         &IntuitionBase,"intuition.library"    ,37, 0,
  68. *         &GfxBase      ,"graphics.library"     ,37, 0,
  69. *         &GadToolsBase ,"gadtools.library"     ,37, 0,
  70. *         &LocaleBase   ,"datatypes.library"    ,39, OLF_OPTIONAL,
  71. *         0,0,0
  72. *       };
  73. *
  74. *       void main(int argc, char **argv) 
  75. *       {
  76. *         if(ex_OpenLibs(arcg,"MyProgram",0,0,0,MyLibs))
  77. *         {
  78. *           ...
  79. *           CloseLibs(MyLibs);
  80. *         }
  81. *       }
  82. *
  83. *   NOTES
  84. *       On error, this function will automatically display an 
  85. *       intuition requester if Argc=0 or print error information 
  86. *       out to STDIO if Argc>0.
  87. *
  88. *       exec.library must already be open.(usually done by the 
  89. *       compiler's startup code)
  90. *
  91. *       revision 1.1
  92. *         autodoc fix
  93. *         now opens exec.library on it's own.
  94. *       revision 1.2
  95. *         renamed to ex_OpenLib due to conflict with reaction.lib
  96. *
  97. *       
  98. *
  99. *   BUGS
  100. *
  101. *   SEE ALSO
  102. *       ex_CloseLibs()
  103. ******************************************************************************
  104. *
  105. */
  106.  
  107. BOOL ex_OpenLibs(ULONG Argc, 
  108.               STRPTR ProgName, 
  109.               STRPTR ErrorString, 
  110.               STRPTR LibVerFmt, 
  111.               STRPTR ButtonText, 
  112.               struct Libs *Libs)
  113. {
  114.   struct ExecBase *SysBase;
  115.   struct Libs *l;
  116.   BOOL rv=TRUE;
  117.  
  118.   SysBase=(struct ExecBase *)(*((ULONG *)4));
  119.  
  120. #ifndef LIBCODE
  121.   if(!ErrorString)
  122.     ErrorString=(STRPTR)"The following libraries are required:\n";
  123.   if(!LibVerFmt)
  124.     LibVerFmt=(STRPTR)"  %s version %ld\n";
  125.   if(!ButtonText)
  126.     ButtonText=(STRPTR)"Ok";
  127. #endif
  128.  
  129.   l=Libs;
  130.   while(l->LibBase)
  131.   {
  132.     *l->LibBase=NULL;
  133.     l++;
  134.   }
  135.  
  136.   l=Libs;
  137.   while(l->LibBase)
  138.   {
  139.     if(!(*l->LibBase=OpenLibrary(l->LibName,l->Version)))
  140.     {
  141.       if(!(l->Flags & OLF_OPTIONAL))
  142.       {
  143.         rv=FALSE;
  144.       }
  145.     }
  146.     l++;
  147.   }
  148.   
  149.   if(!rv)
  150.   {
  151. #ifndef LIBCODE
  152.     if(Argc==0)
  153.     {
  154.       NeedLibs_WB(ProgName,ErrorString,LibVerFmt,ButtonText,Libs);
  155.     }
  156.     else
  157.     {
  158.       NeedLibs_CLI(ProgName,ErrorString,LibVerFmt,Libs);
  159.     }
  160. #endif
  161.     ex_CloseLibs(Libs);
  162.   }
  163.   return(rv);
  164. }
  165.  
  166. #ifndef LIBCODE
  167. void NeedLibs_WB(STRPTR ProgName, 
  168.               STRPTR ErrorString, 
  169.               STRPTR LibVerFmt, 
  170.               STRPTR ButtonText, 
  171.               struct Libs *Libs)
  172. {
  173.   struct IntuitionBase *IntuitionBase;
  174.   UBYTE  *str,*s2;
  175.   ULONG l,len,reqlen,arglen;
  176.   struct EasyStruct es;
  177.  
  178.   IntuitionBase =(struct IntuitionBase *)OpenLibrary((STRPTR)"intuition.library",0);
  179.   
  180.   if(IntuitionBase)
  181.   {
  182.     len=strlen(ErrorString);
  183.     arglen=strlen(LibVerFmt);
  184.     reqlen=len;
  185.     
  186.     l=0;
  187.     while(Libs[l].LibBase)
  188.     {
  189.       if(*Libs[l].LibBase==0 && !(Libs[l].Flags & OLF_OPTIONAL))
  190.       {
  191.         reqlen+=strlen(Libs[l].LibName);
  192.         reqlen+=7;  // for the Version #
  193.         reqlen+=arglen;
  194.       }
  195.       l++;
  196.     }
  197.  
  198.     if(str=AllocVec(reqlen+1,MEMF_CLEAR|MEMF_PUBLIC))
  199.     {
  200.       strcpy(str,ErrorString);
  201.       s2=str+len;
  202.  
  203.       l=0;
  204.         
  205.       while(Libs[l].LibBase)
  206.       {
  207.         if(!(*Libs[l].LibBase))
  208.         {
  209.           s2+=sprintf(s2,LibVerFmt,Libs[l].LibName,Libs[l].Version);
  210.         }
  211.         l++;
  212.       }
  213.       es.es_StructSize    =sizeof(struct EasyStruct);
  214.       es.es_Flags         =0;
  215.       es.es_Title         =ProgName;
  216.       es.es_TextFormat    =str;
  217.       es.es_GadgetFormat  =ButtonText;
  218.       EasyRequest(0,&es,0);
  219.       FreeVec(str);
  220.     }
  221.     CloseLibrary((struct Library *)IntuitionBase);
  222.   }
  223. }
  224.  
  225. void NeedLibs_CLI(STRPTR ProgName, 
  226.               STRPTR ErrorString, 
  227.               STRPTR LibVerFmt, 
  228.               struct Libs *Libs)
  229. {
  230.   ULONG l=0;
  231.  
  232.   printf("%s: %s",ProgName,ErrorString);
  233.   while(Libs[l].LibBase)
  234.   {
  235.     if(!(*Libs[l].LibBase))
  236.     {
  237.       printf(LibVerFmt,Libs[l].LibName,Libs[l].Version);
  238.     }
  239.     l++;
  240.   }
  241.   printf("\n");
  242. }
  243.  
  244. #endif